package com.zx.auth.block.controller;

import com.zx.auth.block.param.OAuth2AuthorizationParam;
import com.zx.auth.block.service.IOAuth2AuthorizationService;
import com.zx.auth.block.vo.OAuth2AuthorizationVO;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * OAuth2授权令牌管理控制器
 * 提供OAuth2授权令牌的查询和管理功能
 */
@RestController
@RequestMapping("/oauth2/authorization")
@RequiredArgsConstructor
public class OAuth2AuthorizationController {

    private final IOAuth2AuthorizationService oAuth2AuthorizationService;

    /**
     * 根据ID获取OAuth2授权令牌信息
     *
     * @param id 授权令牌ID
     * @return 授权令牌信息
     */
    @PreAuthorize("hasAuthority('oauth2:authorization:info')")
    @OperateLog(value = "获取授权令牌信息")
    @GetMapping("/info/{id}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "授权令牌ID", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ObjectWrapper<OAuth2AuthorizationVO> getAuthorizationById(@PathVariable String id) {
        return oAuth2AuthorizationService.getAuthorizationById(id);
    }

    /**
     * 分页查询OAuth2授权令牌列表
     *
     * @param authorizationParam 授权令牌参数
     * @return 授权令牌列表
     */
    @PreAuthorize("hasAuthority('oauth2:authorization:list')")
    @OperateLog(value = "分页查询授权令牌")
    @PostMapping("/list")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "授权令牌参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Query.class})
    )
    public PageWrapper<OAuth2AuthorizationVO> listAuthorizations(@RequestBody OAuth2AuthorizationParam authorizationParam) {
        return oAuth2AuthorizationService.listAuthorizations(authorizationParam);
    }

    /**
     * 删除OAuth2授权令牌
     *
     * @param id 授权令牌ID
     * @return 删除结果
     */
    @PreAuthorize("hasAuthority('oauth2:authorization:remove')")
    @OperateLog(value = "删除授权令牌")
    @DeleteMapping("/deleteAuthorization/{id}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "授权令牌ID", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ResponseWrapper deleteAuthorization(@PathVariable String id) {
        return oAuth2AuthorizationService.deleteAuthorization(id);
    }
}